using System.Collections;
using System.Collections.ObjectModel;
using CodeGears.WorkItemTracking.ChartingDashboard.Domain;

namespace CodeGears.WorkItemTracking.ChartingDashboard
{
    internal class WorkItemsService : IWorkItemsService
    {
        private Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore WorkItemStore { get; set; }

        public WorkItemsService(Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore workItemStore)
        {
            WorkItemStore = workItemStore;
        }

        public ObservableCollection<Project> GetProjects()
        {
            var projects = new ObservableCollection<Project>();
            foreach (Microsoft.TeamFoundation.WorkItemTracking.Client.Project project in this.WorkItemStore.Projects)
            {
                projects.Add(new Project(project));
            }

            return projects;
        }

        public QueryResult RunQuery(Query query, Project project)
        {
            var queryResult = new QueryResult();

            if (query == null || project == null) return queryResult;

            var parameters = new Hashtable {{"project", project.TfsProject.Name}};
            var result = this.WorkItemStore.Query(query.TfsQuery.QueryText, parameters);

            foreach (Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem workItem in result)
            {
                queryResult.Add(workItem);
            }

            return queryResult;
        }
    }
} 